package com.mxprojects.constructors
{
	import com.mxprojects.gui.BaseForumElement;
	import com.mxprojects.gui.Styles;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	
	import flash.display.MovieClip;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	public class CheckboxConstructor extends BaseForumElement implements IEventDispatcher
	{
		public static const TOGGLE:String = "toggle";
		
		private const LABEL_MARGIN:int = 5;
		
		public var check:MovieClip;
		public var isChecked:Boolean = false;
		public var value:String;
		public var label:SimpleTextField;
		
		private var boxCSS:Object;
		
		public function CheckboxConstructor(text:String, checkedValue:String, checkedByDefault:Boolean = false, 
											textLinkListener:Function = null, labelStyle:String = "checkboxText"){
			//build the checkbox based around the checkbox from the library
			if(checkedByDefault){
				check.visible = true;
				isChecked = true;
			}else{
				check.visible = false;
			}
			
			//set the value
			this.value = checkedValue;
			
			//get the box width/height
			boxCSS = Styles.css.getStyle(".checkboxBackground");
			
			//move the check to the padding
			check.x = Math.round(boxCSS.width/2 - check.width/2);
			check.y = Math.round(boxCSS.height/2 - check.height/2);
			check.mouseEnabled = false;
			Styles.setProperties(check, ".checkboxCheckmark");
			
			//add the clickable box
			background = createBox();
			background.addEventListener(MouseEvent.CLICK, toggleCheck, false, 0, true);
			addChildAt(background, 0);
			
			//add the label
			label = createLabel("<span class=\"" + labelStyle + "\">" + text + "</span>", textLinkListener);
			label.x = background.width + LABEL_MARGIN;
			label.y = (background.height/2) - (label.height/2);
			
			//add a listener to the label so it fires the checkbox
			label.addEventListener(MouseEvent.CLICK, toggleCheck, false, 0, true);
			addChild(label);
			
			//do the css
			super.init(".checkboxBackground", 0, false);
		}
		
		public function getValueIfChecked():String {
			//this will return "" if not checked and the value if it is
			if(isChecked){
				return value;
			}else{
				return "";
			}
		}
		
		public function setCheckState(isChecked:Boolean):void {
			//set the local var
			this.isChecked = isChecked;
			
			//set the state
			if(isChecked){
				//show
				check.visible = true;
			}else{
				//hide
				check.visible = false;
			}
		}
		
		override public function get height():Number {
			//this will make it so that it returns the height of the box instead of the text block
			return box.height;
		}
		
		override public function set enabled(isEnabled:Boolean):void {
			//this will remove any event listeners and such if it's disabled
			if(isEnabled){
				alpha = 1;
				if(!background.hasEventListener(MouseEvent.CLICK)){
					background.addEventListener(MouseEvent.CLICK, toggleCheck, false, 0, true);
					label.addEventListener(MouseEvent.CLICK, toggleCheck, false, 0, true);
				}
			}else{
				alpha = Config.BUTTON_DISABLED_ALPHA;
				background.removeEventListener(MouseEvent.CLICK, toggleCheck);
				label.removeEventListener(MouseEvent.CLICK, toggleCheck);
			}
		}
		
		private function createBox():MovieClip {
			//this makes the box around the check icon
			var box:MovieClip = new MovieClip();
			box.graphics.beginFill(uint(boxCSS.color));
			box.graphics.drawRect(0, 0, int(boxCSS.width), int(boxCSS.height));
			box.graphics.endFill();
			
			return box;
		}
		
		private function createLabel(text:String, labelListener:Function = null):SimpleTextField {
			//setup the label
			var labelText:SimpleTextField = new SimpleTextField(text);
			
			if(labelListener != null){
				//add a listener
				labelText.addEventListener(TextEvent.LINK, labelListener, false, 0, true);
			}
			
			return labelText;
		}
		
		private function toggleCheck(event:MouseEvent):void {
			//toggle the checkbox and set the var
			if(isChecked){
				//hide stuff
				check.visible = false;
				isChecked = false;
			}else{
				//show stuff
				check.visible = true;
				isChecked = true;
			}
			
			//if anyone is listening tell them that we've toggled
			dispatchEvent(new CustomEvent(TOGGLE, isChecked, value));
		}

	}
}